package ljl.alg.wangzheng_camp.round1.array_and_list;

import commons.ListNode;

import java.util.*;

public class _148_sort_link_list {
    
    /**
     * 这个题
     * 搞什么花里胡哨的都没用
     * 明明放到数组里，然后 arrays.sort
     * 再恢复成链表，是最快的
     *
     * 看了答案发觉这个解法还挺好的。。
     *
     * */
    public ListNode sortList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode slow = head, fast = head;
        while (fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode list1 = head;
        ListNode list2 = slow.next;
        slow.next = null;
    
        list1 = sortList(list1);
        list2 = sortList(list2);
        ListNode dummy = new ListNode();
        ListNode p = dummy;
        while (list1 != null && list2 != null) {
            int v1 = list1.val, v2 = list2.val;
            if (v1 <= v2) {
                p.next = list1;
                list1 = list1.next;
            } else {
                p.next = list2;
                list2 = list2.next;
            }
            p = p.next;
        }
        p.next = list1 == null ? list2 : list1;
        return dummy.next;
    }
    
    /*
    * 临死前写个迭代法
    * */
    public ListNode sortList1(ListNode head) {
        int count = 0;
        ListNode p = head;
        while (p != null) {
            count++;
            p = p.next;
        }
        ListNode dummy = new ListNode(0, head);
        for (int span = 1; span < count; span <<= 1) {
            ListNode prev = dummy, cur = dummy.next;
            while (cur != null) {
                ListNode head1 = cur;
                int i;
                for (i = 1; i < span && cur.next != null; i++) {
                    cur = cur.next;
                }
                ListNode head2 = cur.next;
                cur.next = null;
                cur = head2;
                for (i = 1; i < span && cur != null && cur.next != null; i++) {
                    cur = cur.next;
                }
                ListNode next = null;
                if (cur != null) {
                    next = cur.next;
                    cur.next = null;
                }
                prev.next = merge(head1, head2);
                while (prev.next != null) {
                    prev = prev.next;
                }
                cur = next;
            }
        }
        return dummy.next;
    }
    
    ListNode merge(ListNode head1, ListNode head2) {
        ListNode fake = new ListNode();
        ListNode cur = fake;
        while (head1 != null && head2 != null) {
            int v1 = head1.val, v2 = head2.val;
            if (v1 <= v2) {
                cur.next = head1;
                head1 = head1.next;
            } else {
                cur.next = head2;
                head2 = head2.next;
            }
            cur = cur.next;
        }
        cur.next = head1 == null ? head2 : head1;
        return fake.next;
    }
    
    /*
     * 试试我最初的猜想快不快
     * */
    public ListNode sortList2(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode p = head;
        Map<Integer, List<ListNode>> sameValueMap = new HashMap<>();
        SortedSet<Integer> sortedSet = new TreeSet<>();
        while (p != null) {
            sameValueMap.computeIfAbsent(p.val, k -> new ArrayList<>()).add(p);
            sortedSet.add(p.val);
            p = p.next;
        }
        ListNode dummy = new ListNode();
        p = dummy;
    
        for (Integer val :sortedSet){
            for (ListNode node : sameValueMap.get(val)) {
                p.next = node;
                p = p.next;
            }
        }
        
        return dummy.next;
    }
}
